package org.apm.data.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.chainsaw.Main;

public final class TimeUtil {
	private static final Logger LOGGER = LogManager.getLogger(TimeUtil.class);
	private static final int UNIT_CONVERSION = 1000000; // between ms and ns;

	private static final int SECOND = 1000;
	private static final int MINUTE = 60 * 1000;
	private static final int HOUR = 60 * 60 * 1000;

	private TimeUtil() {
	}

	public static Calendar timestampToCalendar(final long timeLong) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date(timeLong));
		return calendar;
	}

	/**
	 * transfer to date in format("yyyy-MM-dd HH:mm:ss").
	 * 
	 * @param timeLong
	 *            timestamp
	 * @return string
	 */
	public static String timestampToDate(final long timeLong) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 定义格式，不显示毫秒
		Timestamp timestamp = new Timestamp(timeLong);// 获取时间
		String dateString = df.format(timestamp);
		return dateString;
	}

	/**
	 * transfer to date in format("yyyy-MM-dd").
	 * 
	 * @param timeLong
	 *            timestamp
	 * @return string
	 */
	public static String timestampToDay(final long timeLong) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 定义格式，不显示毫秒
		Timestamp timestamp = new Timestamp(timeLong);// 获取时间
		String dateString = df.format(timestamp);
		return dateString;
	}

	/**
	 * transfer to date in format("yyyy-MM-dd HH").
	 * 
	 * @param timeLong
	 *            timestamp
	 * @return string
	 */
	public static String timestampToHour(final long timeLong) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH");// 定义格式，不显示毫秒
		Timestamp timestamp = new Timestamp(timeLong);// 获取时间
		String dateString = df.format(timestamp);
		return dateString;
	}

	/**
	 * transfer date string to long type.
	 * 
	 * @param timeString
	 *            date string in format("yyyy-MM-dd HH:mm:ss")
	 * @return long
	 */
	public static long timestampToLong(final String timeString) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 定义格式，不显示毫秒
		try {
			return df.parse(timeString).getTime();
		} catch (ParseException exception) {
			LOGGER.error("Data String to timestamp failed");
			return -1;
		}
	}

	/**
	 * transfer date string to string type.
	 * 
	 * @param timeString
	 *            date string in format("yyyy-MM-dd HH:mm:ss.SSS")
	 * @return string
	 */
	public static String timeToString(final String timeString) {
		SimpleDateFormat ret = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 定义格式，不显示毫秒
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");// 定义格式，不显示毫秒
		try {
			return ret.format(df.parse(timeString));
		} catch (ParseException exception) {
			LOGGER.error("Data String to timestamp failed");
			return "";
		}
	}

	/**
	 * transfer date string to long type.
	 * 
	 * @param timeString
	 *            date string in format("yyyy-MM-dd HH:mm:ss")
	 * @return long
	 */
	public static long timeToLong(final String timeString) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");// 定义格式，不显示毫秒
		try {
			return df.parse(timeString).getTime();
		} catch (ParseException exception) {
			LOGGER.error("Data String to timestamp failed");
			return -1;
		}
	}

	/**
	 * nanosecond to millisecond.
	 * 
	 * @param nanosecond
	 *            nanosecond
	 * @return millisecond
	 */
	public static double toMilesecond(final double nanosecond) {
		return 1.0 * nanosecond / UNIT_CONVERSION;
	}

	public static String millSecondFormat(final long millSecond) {
		String ret = "";
		if (millSecond < MINUTE) {
			int sec = (int) (millSecond / SECOND);
			if (sec == 0) {
				ret = "未知";
			} else {
				ret = String.valueOf(sec) + "秒";
			}
		} else if (millSecond < HOUR) {
			int min = (int) (millSecond / MINUTE);
			int sec = (int) (millSecond % MINUTE);
			ret = String.valueOf(min) + "分";
			if (sec >= SECOND) {
				ret += String.valueOf(sec / SECOND) + "秒";
			}
		} else {
			int hour = (int) (millSecond / HOUR);
			int min = (int) (millSecond % HOUR);
			ret = String.valueOf(hour) + "时";
			if (min >= MINUTE) {
				ret += String.valueOf(min / MINUTE) + "分";
			}
		}
		return ret;
	}

	/**
	 * millisecond to nanosecond.
	 * 
	 * @param millisecond
	 *            millisecond
	 * @return nanosecond
	 */
	public static double toNanosecond(final double millisecond) {
		return 1.0 * millisecond * UNIT_CONVERSION;
	}

	public static void main(String[] args) {
		System.out.println(millSecondFormat(61000));
	}
}
